﻿<Window x:Class="EDMDesigner.Designer.EDMDesignerWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    xmlns:windowsBase="clr-namespace:System.ComponentModel;assembly=WindowsBase"
    xmlns:local="clr-namespace:EDMDesigner.Designer"
    xmlns:localBindindingConverter="clr-namespace:EDMDesigner.Designer.BindingConverter"
    xmlns:uiBusiness="clr-namespace:EDMDesigner.UIBusiness;assembly=UIBusiness"
    xmlns:uiBusinessCSDLType="clr-namespace:EDMDesigner.UIBusiness.CSDL.Type;assembly=UIBusiness"
    xmlns:localControls="clr-namespace:EDMDesigner.Designer.Controls"
    xmlns:localControlsMenu="clr-namespace:EDMDesigner.Designer.Controls.Menu"
    xmlns:localControlsMapping="clr-namespace:EDMDesigner.Designer.Controls.Mapping"
    xmlns:wpfTabControl="clr-namespace:Wpf.Controls;assembly=Wpf.TabControl"
    Title="{x:Static local:EDMDesignerWindow.WindowTitle}"
    Height="600"
    Width="800"
    WindowState="Maximized">

    <Window.Resources>
        <localBindindingConverter:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
        <localBindindingConverter:BoolToFontWeightConverter x:Key="BoolToFontWeightConverter" />
        <ObjectDataProvider x:Key="odp" ObjectType="{x:Type uiBusiness:EDMController}" MethodName="CreateView" />
        <CollectionViewSource x:Key="entityTypesCollectionView"
                              Source="{Binding Source={StaticResource odp}, Path=CSDL.EntityTypes}">
            <CollectionViewSource.SortDescriptions>
                <windowsBase:SortDescription PropertyName="Name" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>
        <CollectionViewSource x:Key="entitySetCollectionView"
                              Source="{Binding Source={StaticResource odp}, Path=CSDL.EntityTypes}">
            <CollectionViewSource.SortDescriptions>
                <windowsBase:SortDescription PropertyName="Name" />
            </CollectionViewSource.SortDescriptions>
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="EntitySetName" />
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
        <CollectionViewSource x:Key="complexTypesCollectionView"
                              Source="{Binding Source={StaticResource odp}, Path=CSDL.ComplexTypes}">
            <CollectionViewSource.SortDescriptions>
                <windowsBase:SortDescription PropertyName="Name" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>
        <CollectionViewSource x:Key="csdlFunctionsCollectionView"
                              Source="{Binding Source={StaticResource odp}, Path=CSDL.Functions}">
            <CollectionViewSource.SortDescriptions>
                <windowsBase:SortDescription PropertyName="Name" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>
        <CollectionViewSource x:Key="tablesCollectionView"
                              Source="{Binding Source={StaticResource odp}, Path=SSDL.Tables}">
            <CollectionViewSource.SortDescriptions>
                <windowsBase:SortDescription PropertyName="Name" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>
        <CollectionViewSource x:Key="ssdlFunctionsCollectionView"
                              Source="{Binding Source={StaticResource odp}, Path=SSDL.Functions}">
            <CollectionViewSource.SortDescriptions>
                <windowsBase:SortDescription PropertyName="Name" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>

        <localControlsMenu:ContextMenu x:Key="CSDLTypeContextMenu">
            <!--<localControlsMenu:MenuItem x:Name="renameMenuItem" Header="{x:Static local:EDMDesignerWindow.RenameText}" Command="{x:Static local:EDMDesignerWindow.RenameCommand}" InputGestureText="F2" />-->
            <localControlsMenu:MenuItem x:Name="deleteMenuItem" Header="{x:Static local:EDMDesignerWindow.DeleteText}" Command="{x:Static local:EDMDesignerWindow.DeleteCommand}" InputGestureText="Del">
                <localControlsMenu:MenuItem.Icon>
                    <Image Source="Controls/CSDLType/Resources/Delete.png" Width="16" Height="16" />
                </localControlsMenu:MenuItem.Icon>
            </localControlsMenu:MenuItem>
            <localControlsMenu:MenuSeparator/>
            <localControlsMenu:MenuItem x:Name="propertiesMenuItem" Header="{x:Static local:EDMDesignerWindow.PropertiesText}" Command="{x:Static local:EDMDesignerWindow.PropertiesCommand}" InputGestureText="F4" />
            <localControlsMenu:MenuItem x:Name="mappingMenuItem" Header="{x:Static local:EDMDesignerWindow.MappingText}" Command="{x:Static local:EDMDesignerWindow.MappingCommand}" InputGestureText="Ctrl+Shift+M" />
            <localControlsMenu:MenuSeparator />
            <localControlsMenu:MenuItem x:Name="showTabsMenuItem" Header="{x:Static local:EDMDesignerWindow.ShowTabsText}" Command="{x:Static local:EDMDesignerWindow.ShowTabsCommand}">
                <localControlsMenu:MenuItem.ItemTemplate>
                    <DataTemplate>
                        <MenuItem Command="{x:Static local:EDMDesignerWindow.ShowTabsItemCommand}" CommandParameter="{Binding}">
                            <MenuItem.Header>
                                <Grid Background="DarkGray" Margin="-30,0,-120,0">
                                    <StackPanel>
                                        <TextBlock Margin="10,5" x:Name="tabTextBlock" Text="{Binding Name}" Background="DarkGray" Foreground="White" FontWeight="Bold" />
                                        <Grid Margin="2">
                                            <localControls:DesignerCanvasPreview x:Name="tabDesignerCanvas" Width="150" Height="100" DesignerView="{Binding}" />
                                            <Grid Opacity="0" Background="White" Width="150" Height="100" />
                                        </Grid>
                                    </StackPanel>
                                </Grid>
                            </MenuItem.Header>
                        </MenuItem>
                    </DataTemplate>
                </localControlsMenu:MenuItem.ItemTemplate>
            </localControlsMenu:MenuItem>
        </localControlsMenu:ContextMenu>

        <InputBindingCollection x:Key="ContextMenuKeyBindings">
            <KeyBinding Key="Delete" Command="{x:Static local:EDMDesignerWindow.DeleteCommand}" />
            <KeyBinding Key="F4" Command="{x:Static local:EDMDesignerWindow.PropertiesCommand}" />
            <KeyBinding Key="M" Modifiers="CTRL+Shift" Command="{x:Static local:EDMDesignerWindow.MappingCommand}" />
        </InputBindingCollection>
    </Window.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="300"/>
        </Grid.ColumnDefinitions>

        <wpfTabControl:TabControl x:Name="designerTab" ItemsSource="{Binding Source={StaticResource odp}, Path=DesignerViews}" Grid.Column="0" Margin="0,0,2,0" MouseLeave="DesignerTab_MouseLeave" NewTabItem="designerTab_NewTabItem">
            <wpfTabControl:TabControl.ItemTemplate>
                <DataTemplate>
                    <localControls:EditableTextBlock x:Name="tabNameEditableTextBlock" Text="{Binding Name, Mode=TwoWay}"/>
                </DataTemplate>
            </wpfTabControl:TabControl.ItemTemplate>
            <!--<wpfTabControl:TabControl.ContentTemplate>
                <DataTemplate>
                    <ScrollViewer x:Name="designerScrollViewer" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden">
                        <localControls:DesignerCanvas x:Name="designerSurface" AllowDrop="True" EDMView="{Binding Source={StaticResource odp}}" DesignerView="{Binding}" Background="White" />
                    </ScrollViewer>
                </DataTemplate>
            </wpfTabControl:TabControl.ContentTemplate>-->
        </wpfTabControl:TabControl>
        <GridSplitter Width="2" BorderBrush="DarkGray" Margin="0,0,-1,0"/>
        <ScrollViewer Grid.Column="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
            <TabControl x:Name="toolBoxTabControl" IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Stretch" TabStripPlacement="Bottom">
                <TabItem Header="{x:Static local:EDMDesignerWindow.ModelBrowserTabText}">
                    <Grid x:Name="toolbox" Grid.Column="0" Background="LightGray">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <localControls:ToolBoxExpander Header="{x:Static local:EDMDesignerWindow.ModelBrowserTabCSDLGroupText}" Grid.Row="0" IsExpanded="True" Margin="5,5,5,5" Background="Gray">
                            <ScrollViewer VerticalScrollBarVisibility="Auto">
                                <localControls:ToolBoxGrid LastRowMinHeight="65">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>
                                    <localControls:ToolBoxExpander Grid.Row="0" Header="{x:Static local:EDMDesignerWindow.ModelBrowserTabCSDLGroupEntityTypeGroupText}" Margin="5,5,5,5" Padding="2,2,2,2" IsExpanded="True" Background="DarkGray">
                                        <Grid Background="LightGray">
                                            <Grid>
                                                <RadioButton x:Name="groupRadioButton" Margin="2,2,2,0" Content="{x:Static local:EDMDesignerWindow.ModelBrowserTabCSDLGroupEntityTypeGroupRadioGroupByEntitySetText}" IsChecked="True" />
                                                <RadioButton x:Name="hierarchyRadioButton" Margin="2,25,2,0" Content="{x:Static local:EDMDesignerWindow.ModelBrowserTabCSDLGroupEntityTypeGroupRadioEntitySetHierarchyText}" />
                                            </Grid>
                                            <localControls:InputBindingsContener Margin="0,50,0,2">
                                                <ListView x:Name="entityTypesListView" Margin="2,0,2,0" MinHeight="20" Visibility="{Binding ElementName=groupRadioButton, Path=IsChecked, Converter={StaticResource BoolToVisibilityConverter}, Mode=OneWay}" ItemsSource="{Binding Source={StaticResource entitySetCollectionView}}" ItemTemplate="{DynamicResource CSDLTypeItemTemplate}" SelectionMode="Single" ContextMenu="{StaticResource CSDLTypeContextMenu}" localControls:InputBindingsContener.InputBindingsCollection="{StaticResource ContextMenuKeyBindings}" SelectionChanged="EntityTypesListView_SelectionChanged" GotFocus="EntityTypesListView_GotFocus" >
                                                    <ListView.GroupStyle>
                                                        <GroupStyle>
                                                            <GroupStyle.HeaderTemplate>
                                                                <DataTemplate>
                                                                    <Grid Background="LightGray">
                                                                        <TextBlock Text="{Binding Name}" />
                                                                    </Grid>
                                                                </DataTemplate>
                                                            </GroupStyle.HeaderTemplate>
                                                        </GroupStyle>
                                                    </ListView.GroupStyle>
                                                </ListView>
                                            </localControls:InputBindingsContener>
                                            <Grid Margin="0,42,0,2" Visibility="{Binding ElementName=hierarchyRadioButton, Path=IsChecked, Converter={StaticResource BoolToVisibilityConverter}, Mode=OneWay}">
                                                <ComboBox x:Name="hierarchyFilterComboBox" Margin="2,5,2,0" Background="White" Height="20" VerticalAlignment="Top" ItemsSource="{Binding Source={StaticResource entityTypesCollectionView}}" SelectedValue="{Binding Source={StaticResource odp}, Path=CSDL.HierarchyType}" DisplayMemberPath="Name" />
                                                <localControls:InputBindingsContener>
                                                    <TreeView x:Name="hierarchyFilterTreeView" Margin="2,30,2,0" MinHeight="20" ItemsSource="{Binding Source={StaticResource odp}, Path=CSDL.HierarchyTypes, Mode=OneWay}" ContextMenu="{StaticResource CSDLTypeContextMenu}" localControls:InputBindingsContener.InputBindingsCollection="{StaticResource ContextMenuKeyBindings}" SelectedItemChanged="HierarchyFilterTreeView_SelectedItemChanged" GotFocus="HierarchyFilterTreeView_GotFocus">
                                                        <TreeView.ItemTemplate>
                                                            <HierarchicalDataTemplate DataType="{x:Type uiBusinessCSDLType:UIEntityType}" ItemsSource="{Binding SubEntityTypes}" >
                                                                <localControls:CSDLTypeItem Text="{Binding Name}" UIType="{Binding Mode=OneTime}" FontWeight="{Binding Bold, Converter={StaticResource BoolToFontWeightConverter}, Mode=OneTime}"/>
                                                            </HierarchicalDataTemplate>
                                                        </TreeView.ItemTemplate>
                                                        <TreeView.ItemContainerStyle>
                                                            <Style>
                                                                <Setter Property="TreeViewItem.IsExpanded" Value="true"/>
                                                            </Style>
                                                        </TreeView.ItemContainerStyle>
                                                    </TreeView>
                                                </localControls:InputBindingsContener>
                                            </Grid>
                                        </Grid>
                                    </localControls:ToolBoxExpander>
                                    <GridSplitter Grid.Row="0" Height="2" BorderBrush="Gray" Margin="0,1,0,1" HorizontalAlignment="Stretch" VerticalAlignment="Bottom"/>
                                    <localControls:ToolBoxExpander Grid.Row="1" Header="{x:Static local:EDMDesignerWindow.ModelBrowserTabCSDLGroupComplexTypeGroupText}" Margin="5,1,5,5" Padding="2,2,2,2" IsExpanded="True" Background="DarkGray">
                                        <localControls:InputBindingsContener Margin="2,2,2,2" Background="LightGray">
                                            <ListView x:Name="complexTypesListView" Margin="2,2,2,2" MinHeight="20" ItemsSource="{Binding Source={StaticResource complexTypesCollectionView}}" ItemTemplate="{DynamicResource CSDLTypeItemTemplate}" SelectionMode="Single" ContextMenu="{StaticResource CSDLTypeContextMenu}" localControls:InputBindingsContener.InputBindingsCollection="{StaticResource ContextMenuKeyBindings}" SelectionChanged="ComplexTypesListView_SelectionChanged" GotFocus="ComplexTypesListView_GotFocus" />
                                        </localControls:InputBindingsContener>
                                    </localControls:ToolBoxExpander>
                                    <GridSplitter Grid.Row="1" Height="2" BorderBrush="Gray" Margin="0,1,0,1" HorizontalAlignment="Stretch" VerticalAlignment="Bottom"/>
                                    <localControls:ToolBoxExpander Grid.Row="2" Header="{x:Static local:EDMDesignerWindow.ModelBrowserTabCSDLGroupFunctionsGroupText}" Margin="5,1,5,5" Padding="2,2,2,2" IsExpanded="True" Background="DarkGray">
                                        <Grid Margin="2,2,2,2" Background="LightGray">
                                            <ListView Margin="2,2,2,2" MinHeight="20" ItemsSource="{Binding Source={StaticResource csdlFunctionsCollectionView}}" DisplayMemberPath="Name">
                                            </ListView>
                                        </Grid>
                                    </localControls:ToolBoxExpander>
                                </localControls:ToolBoxGrid>
                            </ScrollViewer>
                        </localControls:ToolBoxExpander>
                        <GridSplitter Grid.Row="0" Height="2" BorderBrush="Gray" Margin="0,1,0,1" HorizontalAlignment="Stretch" VerticalAlignment="Bottom"/>
                        <localControls:ToolBoxExpander Header="{x:Static local:EDMDesignerWindow.ModelBrowserTabSSDLGroupText}" Grid.Row="1" Margin="5,1,5,5" IsExpanded="True" Background="Gray">
                            <ScrollViewer VerticalScrollBarVisibility="Auto">
                                <localControls:ToolBoxGrid LastRowMinHeight="65">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>
                                    <localControls:ToolBoxExpander Grid.Row="0" Header="{x:Static local:EDMDesignerWindow.ModelBrowserTabSSDLGroupEntityTypeGroupText}" Margin="5,1,5,5" Padding="2,2,2,2" IsExpanded="True" Background="DarkGray">
                                        <Grid Margin="2,2,2,2" Background="LightGray">
                                            <ListView Margin="2,2,2,2" MinHeight="20" ItemsSource="{Binding Source={StaticResource tablesCollectionView}}" DisplayMemberPath="Name">
                                            </ListView>
                                        </Grid>
                                    </localControls:ToolBoxExpander>
                                    <GridSplitter Grid.Row="0" Height="2" BorderBrush="Gray" Margin="0,1,0,1" HorizontalAlignment="Stretch" VerticalAlignment="Bottom"/>
                                    <localControls:ToolBoxExpander Grid.Row="1" Header="{x:Static local:EDMDesignerWindow.ModelBrowserTabSSDLGroupFunctionsGroupText}" Margin="5,1,5,5" Padding="2,2,2,2" IsExpanded="True" Background="DarkGray">
                                        <Grid Margin="2,2,2,2" Background="LightGray">
                                            <ListView Margin="2,2,2,2" MinHeight="20" ItemsSource="{Binding Source={StaticResource ssdlFunctionsCollectionView}}" DisplayMemberPath="Name">
                                            </ListView>
                                        </Grid>
                                    </localControls:ToolBoxExpander>
                                </localControls:ToolBoxGrid>
                            </ScrollViewer>
                        </localControls:ToolBoxExpander>
                        <GridSplitter Grid.Row="1" Height="2" BorderBrush="Gray" Margin="0,1,0,1" HorizontalAlignment="Stretch" VerticalAlignment="Bottom"/>
                        <Grid Grid.Row="2" />
                    </Grid>
                </TabItem>
                <TabItem x:Name="propertiesTabItem" Header="{x:Static local:EDMDesignerWindow.TabPropertiesText}">
                    <localControls:PropertyGrid x:Name="propertiesGrid" CSDL="{Binding Source={StaticResource odp}, Path=CSDL.CSDL}" HorizontalAlignment="Stretch" />
                </TabItem>
                <TabItem x:Name="mappingTabItem" Header="{x:Static local:EDMDesignerWindow.TabMappingText}">
                    <localControlsMapping:Mapping x:Name="mapping" EDM="{Binding Source={StaticResource odp}, Path=EDM}"/>
                </TabItem>
                <TabItem Header="Temp">
                    <Button Content="Save" Click="SaveButton_Click" />
                </TabItem>
            </TabControl>
        </ScrollViewer>
    </Grid>
</Window>
